<?php
namespace app\admin\controller;

use app\admin\model\User as UserModel;
use think\Request;

class User extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }
    public function index()
    {
        return $this->fetch('public/help');
    }

    public function userList(){
        $user = new UserModel();
        $rs = $user->getAllPage();
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function userInfo(Request $request){
        if($request->post()){
            $data = $request->post();
            $user = new UserModel();
            //有图片则上传并删除原图
            if($request->file('pic1')){
                //上传图片
                $file = $request->file('pic1');
                $uploadFile = new UploadFile();
                $file = $uploadFile->oneFile('./static/uploads',$file);
                if($file['code'] == 0){
                    return $this->error($file['msg']);
                }
                //删除原图
                $where['id'] = $data['id'];
                $user_info = $user->getOne($where);
                if (!unlink('./static/uploads'.$user_info['licence_img_src'])){
                    return $this->error('替换文件失败');
                }else{
                    $data['licence_img_src'] = $file['msg'];
                }
            }
            //无图修改数据
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $user->edit($where,$data);
            if($rs){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }else{
            $data = $request->get();
            $user = new UserModel();
            $where['id'] = $data['user_id'];
            $rs = $user->getOne($where);
            $this->assign('rs',$rs);
            return $this->fetch();
        }
    }

    public function editUser(Request $request){
        $data = $request->post();
        dump($data);die;
    }
}